//
//  XGUpdatePasswordViewController.m
//  Instagram
//
//  Created by monkey on 2019/4/20.
//  Copyright © 2019 itcast. All rights reserved.
//

#import <SVProgressHUD/SVProgressHUD.h>

#import "XGUpdatePasswordViewController.h"

@interface XGUpdatePasswordViewController ()

/// 原密码
@property (nonatomic,strong) UITextField *originalPasswordTextField;
/// 新密码
@property (nonatomic,strong) UITextField *passwordTextField;
/// 确认密码
@property (nonatomic,strong) UITextField *confirmPasswordTextField;
/// 确定按钮
@property (nonatomic,strong) UIButton *okButton;
/// 取消按钮
@property (nonatomic,strong) UIButton *cancelButton;

@end

@implementation XGUpdatePasswordViewController

#pragma mark - 控制器生命周期方法

- (void)loadView
{
    [super loadView];
    
    [self setUpUI];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 设置导航栏标题
    self.navigationItem.title = @"修改密码";
    
    // 添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchViewForHiddenKeyboardAction)];
    [self.view addGestureRecognizer:tap];
}

#pragma mark - 事件监听

/// 触摸视图隐藏键盘
- (void)touchViewForHiddenKeyboardAction
{
    [self.view endEditing:YES];
}

/// 取消按钮事件
- (void)cancelButtonAction
{
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

/// 提交按钮事件
- (void)okButtonAction
{
    // 退出键盘
    [self.view endEditing:YES];
    
    // 验证字段是否为空
    if (!_originalPasswordTextField.hasText || !_passwordTextField.hasText || !_confirmPasswordTextField.hasText) {
        [SVProgressHUD showErrorWithStatus:@"请填写所有字段"];
        return;
    }
    
    // 验证密码长度 需要 >= 6 <= 10
    if (_passwordTextField.text.length < 6 || _passwordTextField.text.length > 10) {
        [SVProgressHUD showErrorWithStatus:@"密码长度需要在6~10位"];
        return;
    }
    
    // 验证确认密码
    if (![_confirmPasswordTextField.text isEqualToString:_passwordTextField.text]) {
        [SVProgressHUD showErrorWithStatus:@"密码与确认密码不一致"];
        return;
    }
    
    // 修改密码
    [[XGAccountViewModel currentUser] updatePasswordWithOriginal:_originalPasswordTextField.text newPassword:_passwordTextField.text completion:^(BOOL isSuccess, NSString * _Nonnull message) {
        if (!isSuccess) {
            [SVProgressHUD showErrorWithStatus:message];
            return;
        }
        
        [XGAccountViewModel logOut];
        [SVProgressHUD showSuccessWithStatus:message];
        // 发布通知 切换应用程序根控制器
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:kSwitchApplicationRootViewControllerNotification object:nil];
        });
 
    }];
}

#pragma mark - 其他方法

/// 设置界面
- (void)setUpUI
{
    self.view.backgroundColor = [UIColor red:210 green:254 blue:91 alpha:1];
    
    CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
    CGFloat navigationBarHeight = statusBarHeight + 44.f;
    
    // 添加子控件
    [self.view addSubview:self.originalPasswordTextField];
    [self.view addSubview:self.passwordTextField];
    [self.view addSubview:self.confirmPasswordTextField];
    [self.view addSubview:self.cancelButton];
    [self.view addSubview:self.okButton];
    
    // 容器视图子控件布局
    CGFloat textFieldWidth = self.view.width - 20 * 2;
    CGFloat textFieldHeight = 36;
    _originalPasswordTextField.frame = CGRectMake(20, navigationBarHeight + 44, textFieldWidth, textFieldHeight);
    _passwordTextField.frame = CGRectMake(20, _originalPasswordTextField.maxY + 15, textFieldWidth, textFieldHeight);
    _confirmPasswordTextField.frame = CGRectMake(20, _passwordTextField.maxY + 15, textFieldWidth, textFieldHeight);
    _okButton.frame = CGRectMake(20, _confirmPasswordTextField.maxY + 15, 80, 36);
    _cancelButton.frame = CGRectMake(self.view.width - 80 - 20, _confirmPasswordTextField.maxY + 15, 80, 36);
}

#pragma mark - 懒加载

- (UITextField *)originalPasswordTextField
{
    if (!_originalPasswordTextField) {
        _originalPasswordTextField = [UITextField textFieldWithPlaceholder:@"原密码(必填)" fontSize:15 textColor:[UIColor darkTextColor] isSecureTextEntry:YES keyboardType:UIKeyboardTypeDefault];
    }
    
    return _originalPasswordTextField;
}

- (UITextField *)passwordTextField
{
    if (!_passwordTextField) {
        _passwordTextField = [UITextField textFieldWithPlaceholder:@"密码(必填)" fontSize:15 textColor:[UIColor darkTextColor] isSecureTextEntry:YES keyboardType:UIKeyboardTypeDefault];
    }
    
    return _passwordTextField;
}

- (UITextField *)confirmPasswordTextField
{
    if (!_confirmPasswordTextField) {
        _confirmPasswordTextField = [UITextField textFieldWithPlaceholder:@"确认密码(必填)" fontSize:15 textColor:[UIColor darkTextColor] isSecureTextEntry:YES keyboardType:UIKeyboardTypeDefault];
    }
    
    return _confirmPasswordTextField;
}

- (UIButton *)okButton
{
    if (!_okButton) {
        _okButton = [UIButton buttonWithTitle:@"提交" fontSize:15 titleColor:[UIColor whiteColor] target:self action:@selector(okButtonAction)];
        _okButton.backgroundColor = [UIColor orangeColor];
    }
    
    return _okButton;
}

- (UIButton *)cancelButton
{
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithTitle:@"取消" fontSize:15 titleColor:[UIColor whiteColor] target:self action:@selector(cancelButtonAction)];
        _cancelButton.backgroundColor = [UIColor red:245 green:90 blue:93 alpha:1];
    }
    
    return _cancelButton;
}

@end
